//
//  IntegralInfoTableViewCell.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/5/12.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "IntegralInfoTableViewCell.h"

@interface IntegralInfoTableViewCell()

@property (nonatomic,strong) UILabel *timeLabel;
Strong UILabel *titleLabel;

@end

@implementation IntegralInfoTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = 0;
        self.separatorInset = UIEdgeInsetsMake(0, 22, 0, 22);
        self.layoutMargins = UIEdgeInsetsMake(0, 22, 0, 22);
        
        _timeLabel = [UILabel new];
        _timeLabel.font = DEF_FontSize_12;
        
        _titleLabel  = [[UILabel alloc]init];
        _titleLabel.font = DEF_FontSize_12;
        _titleLabel.numberOfLines = 0;
        [self.contentView addSubview:_titleLabel];
        
        [self.contentView addSubview:_timeLabel];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    _titleLabel.frame =  CGRectMake(20 , 0, SCREEN_WIDTH - 100, self.height);
    _timeLabel.frame = CGRectMake(self.width-85, 0, 85, self.height);
}

- (void)setDataDictionary:(NSDictionary *)dataDictionary{
    _dataDictionary = dataDictionary;
    
    _timeLabel.textColor = (self.type == integralType_time ? getColor(@"ff0000") : getColor(@"1b81d0"));
    if (self.type == integralType_time) {
        _timeLabel.text = dataDictionary[@"lis_time"];
        if ([dataDictionary[@"lis_category"] isEqualToString:@"-"]) {
            self.titleLabel.attributedText = [self getAttributeTimeName:[NSString stringWithFormat:@"%@%@",dataDictionary[@"lis_category"],dataDictionary[@"lis_consumption"]] s_name:dataDictionary[@"s_name"] c_name:dataDictionary[@"c_name"]];
        }else{
            self.titleLabel.attributedText = [self getAttributeTime1Name:[NSString stringWithFormat:@"%@%@",dataDictionary[@"lis_category"],dataDictionary[@"lis_get"]]];
        }
    }else{
        _timeLabel.text = dataDictionary[@"pis_time"];
        if ([dataDictionary[@"pis_category"] isEqualToString:@"-"]) {
            self.titleLabel.attributedText = [self getAttributeEver:[NSString stringWithFormat:@"+%@",dataDictionary[@"pis_get"]] s_name:dataDictionary[@"s_name"] c_name:dataDictionary[@"c_name"] name:[NSString stringWithFormat:@"%@%@",dataDictionary[@"pis_category"],dataDictionary[@"pis_consumption"]]];
        }else{
            self.titleLabel.attributedText = [self getAttributeEver1Name:[NSString stringWithFormat:@"%@%@",dataDictionary[@"pis_category"],dataDictionary[@"pis_get"]]];
        }
    }
    
}

- (NSAttributedString *)getAttributeEver1Name:(NSString *)name{
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
    NSAttributedString *nameAtt = [[NSAttributedString alloc]initWithString:name attributes:@{NSForegroundColorAttributeName:getColor(@"1b81d0")}];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"本日获取积分为"]];
    [attStr appendAttributedString:nameAtt];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"分"]];
    
    return attStr;
}

- (NSAttributedString *)getAttributeEver:(NSString *)getMoney s_name:(NSString *)s_name c_name:(NSString *)c_name  name:(NSString *)name {
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
     NSAttributedString *getMoneyAtt = [[NSAttributedString alloc]initWithString:getMoney attributes:@{NSForegroundColorAttributeName:getColor(@"1b81d0")}];
    NSAttributedString *nameAtt = [[NSAttributedString alloc]initWithString:name attributes:@{NSForegroundColorAttributeName:getColor(@"1b81d0")}];
    NSAttributedString *s_nameAtt = [[NSAttributedString alloc]initWithString:s_name];
    NSAttributedString *c_nameAtt = [[NSAttributedString alloc]initWithString:c_name];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"本店获得积分为"]];
    [attStr appendAttributedString:getMoneyAtt];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"分"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"消费店铺: "]];
    [attStr appendAttributedString:s_nameAtt];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"购买商品: "]];
    [attStr appendAttributedString:c_nameAtt];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"消费积分为"]];
    [attStr appendAttributedString:nameAtt];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"分"]];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:3];//调整行间距
    
    [attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attStr.mutableString length])];
    
    return attStr;
}

- (NSAttributedString *)getAttributeTime1Name:(NSString *)name{
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
    NSAttributedString *nameAtt = [[NSAttributedString alloc]initWithString:name attributes:@{NSForegroundColorAttributeName:getColor(@"ff0000")}];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"本日领取限时积分为"]];
    [attStr appendAttributedString:nameAtt];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"分"]];
    
    return attStr;
}

- (NSAttributedString *)getAttributeTimeName:(NSString *)name s_name:(NSString *)s_name c_name:(NSString *)c_name{
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
    NSAttributedString *nameAtt = [[NSAttributedString alloc]initWithString:name attributes:@{NSForegroundColorAttributeName:getColor(@"ff0000")}];
    NSAttributedString *s_nameAtt = [[NSAttributedString alloc]initWithString:s_name];
    NSAttributedString *c_nameAtt = [[NSAttributedString alloc]initWithString:c_name];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"本次消费积分为"]];
    [attStr appendAttributedString:nameAtt];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"分"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"消费店铺: "]];
    [attStr appendAttributedString:s_nameAtt];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"购买商品: "]];
    [attStr appendAttributedString:c_nameAtt];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:4];//调整行间距
    
    [attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attStr.mutableString length])];
    
    return attStr;
}

@end
